package initialize

import (
	"GoodOrder/global"
	"GoodOrder/model"
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"log"
	"os"
)

func InitEs() {
	//开启日志
	logger := log.New(os.Stderr, "test", log.LstdFlags)
	//初始化Es
	global.Es, _ = elastic.NewClient(elastic.SetURL(
		fmt.Sprintf("http://%s:%d", global.ServerConfig.EsConfig.Host,
			global.ServerConfig.EsConfig.Port)),
		elastic.SetSniff(false), elastic.SetTraceLog(logger),
	)
	//直接将本模块的业务创建对应的es索引
	//判断es中是否有此业务的索引
	exits, err := global.Es.IndexExists(model.EsOrder{}.GetIndexName()).Do(context.Background())
	if err != nil {
		panic(err)
	}
	if !exits {
		_, errs := global.Es.CreateIndex(model.EsOrder{}.GetIndexName()).Do(context.Background())
		if errs != nil {
			panic(errs)
		}
	}
}
